package org.indi.yudidi.pojo;

import org.springframework.beans.factory.InitializingBean;

/**
 * 测试生命周期的Bean
 */
public class StudentBean implements InitializingBean{
    private String name;
    private int age;

    public StudentBean(){
        System.out.println("【Bean构造方法】StudentBean()");
        System.out.println(">>>>>>>>>>>>> bean实例化完成");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【set注入】注入学生的name属性:"+name);
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        System.out.println("【set注入】注入学生的age属性:"+age);
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentBean{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(">>>>>>>>>>> bean初始化完成,可以开展配置检查工作");
        System.out.println("【InitializingBean接口】调用InitializingBean接口的afterPropertiesSet方法");
        System.out.println("【InitializingBean接口】调用路径: initializeBean -> invokeInitMethods(执行初始化方法) -> afterPropertiesSet()");
        System.out.println("【InitializingBean接口】bean初始化赋值完成后,可以调用afterPropertiesSet: 用于检测某些属性是否设置正确,配置不正确可以抛出异常..");
        if (name != null && age != 0){
            System.out.println("【InitializingBean接口】 afterPropertiesSet内检测基本的属性均已成功设置. essential property.");
        }
    }
}